home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / Background / BackgroundGeometry.java.z / BackgroundGeometry.java
Encoding:
Java Source  |  2003-08-08  |  8.1 KB  |  232 lines

  1. /*
  2.  *    @(#)BackgroundGeometry.java 1.11 02/10/21 13:37:48
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import com.sun.j3d.utils.image.TextureLoader;
  41. import com.sun.j3d.utils.behaviors.mouse.*;
  42. import java.applet.Applet;
  43. import java.awt.*;
  44. import java.awt.event.*;
  45. import com.sun.j3d.utils.applet.MainFrame;
  46. import com.sun.j3d.utils.geometry.*;
  47. import com.sun.j3d.utils.universe.*;
  48. import javax.media.j3d.*;
  49. import javax.vecmath.*;
  50.  
  51. public class BackgroundGeometry extends Applet {
  52.  
  53.     BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  54.  
  55.     private java.net.URL bgImage = null;
  56.  
  57.     private SimpleUniverse u = null;
  58.  
  59.     public BranchGroup createSceneGraph() {
  60.  
  61.         // Create the root of the branch graph
  62.         BranchGroup objRoot = new BranchGroup();
  63.  
  64.         // Create a Transformgroup to scale all objects so they
  65.         // appear in the scene.
  66.         TransformGroup objScale = new TransformGroup();
  67.         Transform3D t3d = new Transform3D();
  68.         t3d.setScale(0.4);
  69.         objScale.setTransform(t3d);
  70.         objRoot.addChild(objScale);
  71.  
  72.         // Create the transform group node and initialize it to the
  73.         // identity.  Enable the TRANSFORM_WRITE capability so that
  74.         // our behavior code can modify it at runtime.
  75.         TransformGroup objTrans = new TransformGroup();
  76.         objScale.addChild(objTrans);
  77.     
  78.         Background bg = new Background();
  79.         bg.setApplicationBounds(bounds);
  80.     BranchGroup backGeoBranch = new BranchGroup();
  81.         Sphere sphereObj = new Sphere(1.0f, Sphere.GENERATE_NORMALS |
  82.                       Sphere.GENERATE_NORMALS_INWARD |
  83.                   Sphere.GENERATE_TEXTURE_COORDS, 45);
  84.         Appearance backgroundApp = sphereObj.getAppearance();
  85.         backGeoBranch.addChild(sphereObj);
  86.         bg.setGeometry(backGeoBranch);
  87.         objTrans.addChild(bg);
  88.  
  89.         TextureLoader tex = new TextureLoader(bgImage,
  90.                           new String("RGB"), this);
  91.         if (tex != null) 
  92.         backgroundApp.setTexture(tex.getTexture());
  93.  
  94.         Vector3f tranlation = new Vector3f(2.0f, 0.0f, 0.0f);
  95.         Transform3D modelTransform = new Transform3D();
  96.         Transform3D tmpTransform = new Transform3D();
  97.         double angleInc = Math.PI/8.0;
  98.         double angle = 0.0;
  99.     int numBoxes = 16;
  100.  
  101.         float scaleX[] =  {0.1f, 0.2f, 0.2f, 0.3f,
  102.                            0.2f, 0.1f, 0.2f, 0.3f,
  103.                            0.1f, 0.3f, 0.2f, 0.3f,
  104.                            0.1f, 0.3f, 0.2f, 0.3f};
  105.  
  106.         float scaleY[] =  {0.3f, 0.4f, 0.3f, 0.4f,
  107.                            0.3f, 0.4f, 0.3f, 0.4f,
  108.                            0.3f, 0.3f, 0.3f, 0.3f,
  109.                            0.3f, 0.3f, 0.3f, 0.4f};
  110.  
  111.         float scaleZ[] =  {0.3f, 0.2f, 0.1f, 0.1f,
  112.                            0.3f, 0.2f, 0.1f, 0.3f,
  113.                            0.3f, 0.2f, 0.1f, 0.3f,
  114.                            0.3f, 0.2f, 0.1f, 0.2f};
  115.  
  116.         Appearance a1 = new Appearance();
  117.         Color3f eColor    = new Color3f(0.0f, 0.0f, 0.0f);
  118.         Color3f sColor    = new Color3f(0.5f, 0.5f, 1.0f);
  119.         Color3f oColor    = new Color3f(0.5f, 0.5f, 0.3f);
  120.  
  121.         Material m = new Material(oColor, eColor, oColor, sColor, 100.0f);
  122.         m.setLightingEnable(true);
  123.         a1.setMaterial(m);
  124.  
  125.         for (int i=0; i<numBoxes; i++, angle += angleInc) {
  126.             modelTransform.rotY(angle);
  127.             tmpTransform.set(tranlation);
  128.             modelTransform.mul(tmpTransform);
  129.  
  130.             TransformGroup tgroup = new TransformGroup(modelTransform);
  131.             objTrans.addChild(tgroup);
  132.  
  133.             tgroup.addChild( new Box(scaleX[i],scaleY[i],scaleZ[i],
  134.                 Box.GENERATE_NORMALS,a1));
  135.         }
  136.  
  137.  
  138.         // Shine it with two lights.
  139.         Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
  140.         Color3f lColor2 = new Color3f(0.2f, 0.2f, 0.1f);
  141.         Vector3f lDir1  = new Vector3f(-1.0f, -1.0f, -1.0f);
  142.         Vector3f lDir2  = new Vector3f(0.0f, 0.0f, -1.0f);
  143.         DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
  144.         DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
  145.         lgt1.setInfluencingBounds(bounds);
  146.         lgt2.setInfluencingBounds(bounds);
  147.         objScale.addChild(lgt1);
  148.         objScale.addChild(lgt2);
  149.  
  150.  
  151.         return objRoot;
  152.     }
  153.  
  154.     public BackgroundGeometry (){
  155.     }
  156.  
  157.     public BackgroundGeometry(java.net.URL bgurl) {
  158.         bgImage = bgurl;
  159.     }
  160.  
  161.     public void init() {
  162.  
  163.         if (bgImage == null) {
  164.         // the path to the image for an applet
  165.         try {
  166.             bgImage = new java.net.URL(getCodeBase().toString() +
  167.                        "../images/bg.jpg");
  168.         }
  169.         catch (java.net.MalformedURLException ex) {
  170.             System.out.println(ex.getMessage());
  171.         System.exit(1);
  172.         }
  173.     }
  174.         setLayout(new BorderLayout());
  175.         GraphicsConfiguration config =
  176.         SimpleUniverse.getPreferredConfiguration();
  177.  
  178.         Canvas3D c = new Canvas3D(config);
  179.         add("Center", c);
  180.     
  181.         BranchGroup scene = createSceneGraph();
  182.         u = new SimpleUniverse(c);
  183.  
  184.         // This will move the ViewPlatform back a bit so the
  185.         // objects in the scene can be viewed.
  186.         u.getViewingPlatform().setNominalViewingTransform();
  187.  
  188.         TransformGroup viewTrans =
  189.                 u.getViewingPlatform().getViewPlatformTransform();
  190.  
  191.         // Create the rotate behavior node
  192.         MouseRotate behavior1 = new MouseRotate(viewTrans);
  193.         scene.addChild(behavior1);
  194.         behavior1.setSchedulingBounds(bounds);
  195.  
  196.         // Create the zoom behavior node
  197.         MouseZoom behavior2 = new MouseZoom(viewTrans);
  198.         scene.addChild(behavior2);
  199.         behavior2.setSchedulingBounds(bounds);
  200.  
  201.         // Create the translate behavior node
  202.         MouseTranslate behavior3 = new MouseTranslate(viewTrans);
  203.         scene.addChild(behavior3);
  204.         behavior3.setSchedulingBounds(bounds);
  205.  
  206.         // Let Java 3D perform optimizations on this scene graph.
  207.         scene.compile();
  208.  
  209.         u.addBranchGraph(scene);
  210.     }
  211.  
  212.     public void destroy() {
  213.     u.cleanup();
  214.     }
  215.   
  216.     public static void main(String argv[]) {
  217.       System.out.println("Usage: mouse buttons to rotate, zoom or translate the view platform transform"); 
  218.       System.out.println("       Note that the background geometry only changes with rotation");
  219.       // the path to the image file for an application
  220.       java.net.URL bgurl = null;
  221.       try {
  222.       bgurl = new java.net.URL("file:../images/bg.jpg");
  223.       }
  224.       catch (java.net.MalformedURLException ex) {
  225.       System.out.println(ex.getMessage());
  226.       System.exit(1);
  227.       }
  228.       new MainFrame(new BackgroundGeometry(bgurl), 750, 750);
  229.     }
  230. }
  231.  
  232.